package com.uy.cuantoconocesdeuruguay.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.R;
import com.uy.cuantoconocesdeuruguay.adapters.FriendsViewAdapter;
import com.uy.cuantoconocesdeuruguay.datatypes.FacebookDeviceUser;
import com.uy.cuantoconocesdeuruguay.datatypes.FacebookUser;
import com.uy.cuantoconocesdeuruguay.exceptions.ConnectivityException;
import com.uy.cuantoconocesdeuruguay.server.ServerConnector;
import com.uy.cuantoconocesdeuruguay.utiles.ImageLoader;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class FriendProfileDialogUtils {
	private Activity activity;	
	public FriendProfileDialogUtils(Activity activity) {
		this.activity = activity;
	}

	public void createProfilePopup(final FacebookUser friend){
		if (friend.getScore() == null){
			try {
				FacebookDeviceUser user = ServerConnector.getFriendScore(friend.getId());
				friend.setScore(user.getScore());
				friend.setCorrectImages(user.getCorrectImages());
				friend.setTotalImages(user.getTotalImages());
				friend.setCorrectQuestions(user.getCorrectQuestions());
				friend.setTotalQuestions(user.getTotalQuestions());
				friend.setPercentageCompleted(user.getPercentageCompleted());	
			} catch (ConnectivityException e) {
				ErrorOccurredDialogUtils dialog = new ErrorOccurredDialogUtils(activity, e);
				dialog.createErrorOccurredPopup();
				e.printStackTrace();
			}
		}
		LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    final View friendProfileView = inflater.inflate(R.layout.friend_profile, null, false);
	    final ImageView imgViewer = (ImageView) friendProfileView.findViewById(R.id.friendProfilePicture);
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setView(friendProfileView);
	    builder.setCancelable(true);
	    final AlertDialog dialog = builder.create();
	    
	    if ( !(friend.getPicture() == null || friend.getPicture().equals(StringPool.BLANK)) ){
	    	ProgressBar loader = (ProgressBar)friendProfileView.findViewById(R.id.friendProfileImageProgressBar);
	    	ImageLoader.loadImageOn(activity, imgViewer, "https://graph.facebook.com/"+friend.getId()+"/picture?type=large", loader);
	    }
	    
	    if (friend.getId().equals(MainActivity.userData.getFacebookid())){
	    	LinearLayout friendProfileMainContainer = (LinearLayout)friendProfileView.findViewById(R.id.friendProfileMainContainer);
	    	LinearLayout fiendProfileChallengeContainer = (LinearLayout)friendProfileView.findViewById(R.id.friendProfileChallengeContainer);
	    	friendProfileMainContainer.removeView(fiendProfileChallengeContainer);
	    }else{
	    	LinearLayout challengeIcon = (LinearLayout) friendProfileView.findViewById(R.id.friendProfileChallengeContainer);
		    challengeIcon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					FriendsViewAdapter.challengeFriend(friend.getFirst_name(),friend.getId(),dialog, activity);
				}
			});
	    }
	    TextView profileFriendName = (TextView) friendProfileView.findViewById(R.id.profileFriendName);
	    TextView profileFriendScore = (TextView) friendProfileView.findViewById(R.id.profileFriendScore);
	    
	    TextView profileImagesCorrectTotal = (TextView) friendProfileView.findViewById(R.id.profileImagesCorrectTotal);
	    TextView profileQuestionsCorrectTotal = (TextView) friendProfileView.findViewById(R.id.profileQuestionsCorrectTotal);
	    TextView profilePercentCompleted = (TextView) friendProfileView.findViewById(R.id.profilePercentCompleted);

	    profileFriendName.setText(friend.getFirst_name() + StringPool.SPACE + friend.getLast_name());
	    profileFriendScore.setText(friend.getScore());

	    int imagesEffectiveness = 0;
	    if (friend.getTotalImages() > 0){
	    	imagesEffectiveness = (int) ((float)friend.getCorrectImages()*100.0f/(float)friend.getTotalImages());
	    }
	    profileImagesCorrectTotal.setText(String.valueOf(imagesEffectiveness) + "% ("+friend.getCorrectImages() + StringPool.SLASH + friend.getTotalImages() +")");
	    
	    int questionsEffectiveness = 0;
	    if (friend.getTotalQuestions() > 0){
	    	questionsEffectiveness = (int) ((float)friend.getCorrectQuestions()*100.0f/(float)friend.getTotalQuestions());
	    }
	    profileQuestionsCorrectTotal.setText(String.valueOf(questionsEffectiveness) + "% ("+friend.getCorrectQuestions() + StringPool.SLASH + friend.getTotalQuestions() +")");
	    
	    profilePercentCompleted.setText(String.valueOf(friend.getPercentageCompleted()) + StringPool.PERCENT);
	    
	    dialog.show();
	}
}
